//
//  CYLoopProgressView.m
//  maskProgressDemo
//
//  Created by Mr.GCY on 2017/6/1.
//  Copyright © 2017年 Mr.GCY. All rights reserved.
//

#import "XYLoopProgressView.h"

@implementation XYLoopProgressView
- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGFloat xCenter = rect.size.width * 0.5;
    CGFloat yCenter = rect.size.height * 0.5;
    if (!self.lineColor) {
        self.lineColor = [UIColor orangeColor];
    }
    if (self.lineWidth == 0) {
        self.lineWidth = 10;
    }
    [self.lineColor set];
    
    CGContextSetLineWidth(ctx, self.lineWidth);
    //线头的的形状
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGFloat to = - M_PI * 0.5 + self.progress * M_PI * 2 + 0.05; // 初始值0.05
    CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - self.lineWidth;
    CGContextAddArc(ctx, xCenter, yCenter, radius, - M_PI * 0.5, to, 0);
    CGContextStrokePath(ctx);
    
    if (self.isShowText) {
        // 进度数字
        NSString *progressStr = [NSString stringWithFormat:@"%.0f%%", self.progress * 100];
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        attributes[NSFontAttributeName] = [UIFont boldSystemFontOfSize:10];
        attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
        [self setCenterProgressText:progressStr withAttributes:attributes];
    }
    
}
@end
